"""
    Login.py - Handles new connections
    Copyright (C) 2009  Matthew Baldwin <secretagentcook@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
"""

import Core
import DatabaseAbstractionLayer.DatabaseAbstraction
import Player
import Room


class Login(object):
    """
    Handles log ins and character creation.
    """
    
    def __init__(self):
        self.playerRef = Player.Player.get_sock_ref()
        
    def player_check(self, name): #Remove '\r' at end of name
        self.name = name
        print self.name + " Login.py:32"#this is just for debugging.  Please remove
        self.resultsName = Player.Player.get_name(self.name)
        print str(self.resultsName) + " Login.py:36"#this is for debugging.  Please remove
        if self.resultsName == self.name:
            Player.Player.write_plain("Please enter your password:  /n")
            Player.Player.info['login_state'] = Core.Const.checking_pass
            Player.Player.info['name'] = self.name
        else:
            Player.Player.info['name'] = self.name
            Player.Player.info['login_state'] = Core.Const.creating_name
            Player.Player.write_plain('Is %s what you would like?\r\n' % (self.name))
            Player.Player.info['name'] = self.name
          
            
    def check_pass(self, password):
        self.password_typed = password
        self.name = Player.Player.info['name']
        self.password = Player.Player.get_password(self.name)
        
        if self.password_typed == self.password:
            Player.Player.info['login_state'] = Core.Const.logged_in
            Room.Room.start_room(Player.Player.info['roomNum'])
        else:
            pass
            #TODO:  Allow up to 3 tries.
    
    
    def player_create(self, answer):
        self.answer = answer
        self.name_check = Player.Player.get_name(self.name)

        if self.name_check == self.name:
            Player.Player.write_plain("That name is already taken.")
            #TODO:  Allow for to repeat until a name becomes open
        else:
            Player.Player.info['login_state'] = Core.Const.creating_pass
            Player.Player.write_plain("Please enter a password for %s:  \r\n" % (self.name))
    
    def create_pass(self,  password):
        self.password = password
        Player.Player.info['password'] = self.password
        self.name = Player.Player.info['name']
        
        self.add_to_db = Database.dbAccess.Access.Players(name=self.name,  password=self.password)
        Player.Player.session.add(self.add_to_db)
        Player.Player.session.commit()
        Player.Player.info['login_state'] = Core.Const.logged_in
        Room.Room.start_room(1)

Login = Login()
